package com.suredy.flow;

import java.util.ArrayList;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.suredy.core.helper.ApplicationContextHelper;
import com.suredy.core.mvc.model.Tree;
import com.suredy.flow.model.SenderData;
import com.suredy.flow.model.SenderUnit;
import com.suredy.flow.model.SenderUser;
import com.suredy.security.service.UserSrv;

public class XmlP {

	@SuppressWarnings("rawtypes")
	public SenderData load(String xml, String[] para, String paraType) {
		
		UserSrv bs = (UserSrv) ApplicationContextHelper.getBeanByType(UserSrv.class);
		Document doc = null;
		SenderData senderData = new SenderData();
		try {
			doc = DocumentHelper.parseText(xml);
			
			List sendNodes = doc.selectNodes("/Return/SendArray/Send");

			for (int n = 0; n < sendNodes.size(); n++) {
				Element sendElement = (Element) sendNodes.get(n);
				List elist = sendElement.selectNodes("Receiver");
				List ulist = sendElement.selectNodes("Receiver/OU");
				List glist = sendElement.selectNodes("Receiver/Group");
				String gp = "";
				String up = "";
				String eCurUser = "";
				String eCurGroup = "";
				String excludeCurEditor = "";
				String cu = "";

				if ((elist != null) && (elist.size() > 0)) {
					Element el = (Element) elist.get(0);
					eCurUser = el.attribute("excludeCurUser") == null ? "" : el.attribute("excludeCurUser").getValue();
					eCurGroup = el.attribute("excludeGroup") == null ? "" : el.attribute("excludeGroup").getValue();
					excludeCurEditor = el.attribute("excludeCurEditor") == null ? "" : el.attribute("excludeCurEditor").getValue();
					if ((glist != null) && (glist.size() > 0)) {
						Element e = (Element) glist.get(0);
						gp = e.getText();
					}
					if ((ulist != null) && (ulist.size() > 0)) {
						Element e = (Element) ulist.get(0);
						up = e.getText();
					}
				}
				if (("Y".equals(eCurUser)) && (excludeCurEditor.equals("User"))) {
					List curUser = doc.selectNodes("/Return/User");
					cu = ((Element) curUser.get(0)).attribute("un").getValue();
					
				}
				String sqlStr = CreateQL(gp, up, eCurGroup, cu);
				List list = bs.readBySQL(sqlStr);
				if (list != null) {
					String tl = sendElement.attribute("topLayer").getValue();
					String obj = sendElement.attribute("obj").getValue();
					if ((tl.indexOf("OU") > -1) || (obj.indexOf("OU") > -1)) {
						for (int i = 0; i < list.size(); i++) {
							String[] fullNameSplit = ((Object[]) list.get(i))[4].toString().split("/");
							String unitName = fullNameSplit[(fullNameSplit.length - 2)];
							String unitId = ((Object[]) list.get(i))[1].toString();
							SenderUnit sendUnit = senderData.getUintById(unitId);
							if (sendUnit == null) {
								sendUnit = new SenderUnit();
								sendUnit.setId(unitId);
								sendUnit.setText(unitName);
								sendUnit.setChildren(new ArrayList<Tree>());
								senderData.getUsers().add(sendUnit);
								
							}
							SenderUser senderUser = new SenderUser();
							senderUser.setId(((Object[]) list.get(i))[2].toString());
							senderUser.setText(((Object[]) list.get(i))[3].toString());
							String fullName = ((Object[]) list.get(i))[4].toString();
							senderUser.setUnitName(fullName.substring(fullName.indexOf("/") + 1, fullName.lastIndexOf("/")));

							if (isSelected(para, ((Object[]) list.get(i))[1].toString())) {
								senderUser.setChecked(true);
							}
							sendUnit.getChildren().add(senderUser);
							
						}
						if (senderData.getUsers().size() == 0)
							senderData.setText("无接收用户！");
					} else {
						for (int i = 0; i < list.size(); i++) {
							SenderUser senderUser = new SenderUser();
							senderUser.setId(((Object[]) list.get(i))[2].toString());
							senderUser.setText(((Object[]) list.get(i))[3].toString());
							String fullName = ((Object[]) list.get(i))[4].toString();
							senderUser.setUnitName(fullName.substring(fullName.indexOf("/") + 1, fullName.lastIndexOf("/")));
							senderData.getUsers().add(senderUser);
						}
					}

					senderData.setId(sendElement.attribute("id").getValue());
					senderData.setName(sendElement.attribute("name").getValue());
					senderData.setStyle(sendElement.attribute("style").getValue());
					senderData.setObj(sendElement.attribute("obj").getValue());
					senderData.setTopLayer(sendElement.attribute("topLayer").getValue());
				}
			}
		} catch (DocumentException e) {
			e.printStackTrace();
		}
		return senderData;
	}

	public static String CreateQL(String gp, String up, String eGroup, String cu) {
		String gpstr = "";
		String upstr = "";
		String eGroupstr = "";
		if (gp != "") {
			String[] g = gp.split(";");
			for (int i = 0; i < g.length; i++) {
				if (i == 0)
					gpstr = gpstr + "'" + g[i] + "'";
				else {
					gpstr = gpstr + ",'" + g[i] + "'";
				}
			}
		}
		if (up != "") {
			String[] g = up.split(";");
			for (int i = 0; i < g.length; i++) {
				if (i == 0)
					upstr = upstr + "'" + g[i] + "'";
				else {
					upstr = upstr + ",'" + g[i] + "'";
				}
			}
		}
		if (eGroup != "") {
			String[] g = eGroup.split(";");
			for (int i = 0; i < g.length; i++) {
				if (i == 0)
					eGroupstr = eGroupstr + "'" + g[i] + "'";
				else {
					eGroupstr = eGroupstr + ",'" + g[i] + "'";
				}
			}
		}
		String sql = "";
		if (gpstr == "") {
			sql = sql + "select id_e_yh,wymc_zzdy,wymc,mc,qc from t_e_yh where 1=1 ";
			if (upstr != "")
				sql = sql + " and wymc_zzdy in(" + upstr + ")";
			if (eGroupstr != "")
				sql = sql + "  and wymc not in(select wymc from v_e_qz where groupName in(" + eGroupstr + "))";
			if (cu != "")
				sql = sql + " and  wymc!='" + cu + "'";
			sql = sql + " order by wymc_zzdy,plxh";
		} else {
			sql = sql + "select id_e_yh,wymc_zzdy,wymc,mc,qc from v_e_qz where groupName in (" + gpstr + ") ";
			if (upstr != "")
				sql = sql + " and wymc_zzdy in(" + upstr + ") ";
			if (eGroupstr != "")
				sql = sql + " and groupName not in(" + eGroupstr + ") ";
			if (cu != "")
				sql = sql + " and  wymc!='" + cu + "'";
			sql = sql + " order by wymc_zzdy,plxh";
		}
		return sql;
	}

	public boolean isSelected(String[] para, String id) {
		for (String s : para) {
			if (s.equals(id))
				return true;
		}
		return false;
	}
}
